{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "71785598-eb68-4bea-a23a-67fb9405a4a5",
   "metadata": {},
   "source": [
    "# 区间表示法\n",
    "- 描述区间端点的包含关系\n",
    "- 左闭右开(Closed-Open Interval): 在Python中，通常使用 range() 或 切片语法表示，比如range(start, stop) 或list[start:stop]其中，start 是包含的，而 stop 是不包含的。在数学上，这可以表示为 [start,stop)\n",
    "- 左开右闭(Open-Closed Interval): Python标准库没有直接提供这种区间的表示方法，但可以通过自定义函数或者适当的逻辑来实现，在数学上，这里可以表示为 (start,stop]\n",
    "- 闭区间(Closed Interval): 在Python中，可以通过包含两个端点的列表或 range 函数(如果步长为1)来表示。在数学上，可表示为[start,stop]\n",
    "- 开区间(Open Interval): 在Python中，通常通过适当的逻辑来实现，因为没有直接的语法结构来表示。数学上表示为，(start,stop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c1de5d35-74f8-4bee-8eaa-a39e0b31af54",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1、2、3、4、\n",
      "[20, 30, 40, 50]、\n",
      "2、3、4、5、\n",
      "1、2、3、4、5、\n",
      "2、3、4、"
     ]
    }
   ],
   "source": [
    "#左闭右开(Closed-Open Interval)\n",
    "#使用range()\n",
    "for i in range(1, 5):\n",
    "    print(i, end='、')\n",
    "\n",
    "print()    #换行\n",
    "#使用列表切片\n",
    "list_cut = [10,20,30,40,50,60]\n",
    "print(list_cut[1:5], end='、')\n",
    "\n",
    "print()\n",
    "#左开右闭(Open-Closed Interval)\n",
    "#需要自定义实现，因为Python没有直接支持\n",
    "def open_closed_range(start, stop):\n",
    "    return range(start + 1, stop + 1)\n",
    "\n",
    "for i in open_closed_range(1, 5):\n",
    "    print(i, end='、')\n",
    "\n",
    "print()\n",
    "#闭区间 [1, 5]\n",
    "for i in range(1, 6):    #注意：这里6是stop+1 ,因为range 是左闭右开的\n",
    "    print(i, end='、')\n",
    "\n",
    "print()\n",
    "#开区间 (1, 5)\n",
    "#童谣需要自定义实现\n",
    "def open_interval(start, stop):\n",
    "    return range(start+1, stop)\n",
    "\n",
    "for i in open_interval(1, 5):\n",
    "    print(i, end='、')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
